package cn.jvtc.course.management.system.dao.impl;

import cn.jvtc.course.management.system.bean.ClassInfoDO;
import cn.jvtc.course.management.system.exception.ClassDaoException;
import cn.jvtc.course.management.system.utils.JdbcUtils;

import java.sql.*;

/**
 * 用于对数据库的课程表进行操作
 *
 * @author GaoFeng2017
 * @date 2017-12-10 22:29:03
 **/

public class ClassDaoImpl {

    /**
    * 添加班级
    * @description:
    * @author: GaoFeng2017
    * @date: 2017/12/11 14:55
    * @param: [classInfoDO]
    * @return: boolean
    *
    */


    public boolean addClass(ClassInfoDO classInfoDO) {
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        int i = 0;
        String sql = "insert into class_info(gmt_create,name,grade) values(?,?,?)";

        try{
          con = JdbcUtils.getConnection();
          st = con.prepareStatement(sql);
          st.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
          st.setString(2,classInfoDO.getName());
          st.setShort(3,classInfoDO.getGrade());

          i = st.executeUpdate();
        } catch(Exception e) {
          throw new ClassDaoException(e);
        } finally {
          JdbcUtils.releaseResource(con,st,rs);
        }

        if(i <= 0) {
          return false;
        }
         return true;
    }

    /**
     * 根据班级名字判断该班级是否存在
     * @description:
     * @author: GaoFeng2017
     * @date: 2017/12/11 16:10
     * @param: []
     * @return: cn.jvtc.course.management.system.bean.ClassInfoDO
     *
     */
    public boolean isClassExist(String name) {
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        String sql = "select name from class_info where name = ?";

        try{
            con = JdbcUtils.getConnection();
            st = con.prepareStatement(sql);
            st.setString(1,name);
            rs = st.executeQuery();
            if(!rs.next()) {
                return false;
            }
        } catch(Exception e) {
            throw new ClassDaoException(e);
        } finally {
            JdbcUtils.releaseResource(con,st,rs);
        }
        return true;

    }

    /**
     * 根据班级名字获取班级id
     * @description:
     * @author: GaoFeng2017
     * @date: 2017/12/12 22:07
     * @param: [name]
     * @return: long
     *
     */
    public long getId(String name) {
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        String sql = "select id from class_info where name = ?";
        long id = 0;
        try{
            con = JdbcUtils.getConnection();
            st = con.prepareStatement(sql);
            st.setString(1,name);
            rs = st.executeQuery();
            if(rs.next()) {
                id = rs.getLong("id");
            }
        } catch(Exception e) {
            throw new ClassDaoException(e);
        } finally {
            JdbcUtils.releaseResource(con,st,rs);
        }
        return id;
    }
}
